// confirm.js
var common = require("../../../common/utils.js");
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    distance: "约34km",
    contacts: '',
    contactschoose: '',
    totalmoeny: 0,
    isAgree: true,
    iscontactschoose: false,
    mainpage: true,
    contactpage: false,
    activity: false,
    selected: false,
    appends: false,
    TakeManID: [],
    appColor: app.appColor
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.showLoadings();
    var that = this;
    try {
      wx.getStorage({
        key: 'activitys',
        success: function (res) {
          var active = res.data;
          active.forEach(function (item, i) {
            item.checked = false
          });
          if (active.length > 0) {
            active[0].checked = true;
            that.setData({
              active: active,
              activiId: active[0].ActivityType,
              checked: active[0].checked,
              activename: active[0].ActivityName
            })
          }


        }
      })
    } catch (err) { }
    this.setData({
      price: options.price,
      time: options.time,
      upSite: options.upSite,
      downSite: options.downSite,
      starttime: options.date,

    })

    this.getActivity();
  },

  pres: function (e) {
    app.formID = e.detail.formId;
  },

  pre: function () {
    wx.redirectTo({
      url: '../orderConfirm/orderConfirm?date=' + this.data.starttime + '&time=' + this.data.time + '&activiId=' + this.data.activiId,
    });
  },

  getTakeman: function () {
    var that = this;
    wx.request({
      url: app.serviceurl + 'Member/GetTakeman',
      header: {
        Authorization: "Bearer " + app.accesstoken
      },
      data: {},
      dataType: 'json',
      success: function (res) {
        if (!res.data.Result) {
          app.hideLoadings();
          return
        };
        if (res.data.TakeManList != null && typeof (res.data.TakeManList) != undefined) {
          res.data.TakeManList.forEach(function (item) {
            item.TakeManPhone = item.TakeManPhone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
            item.TakeManCardId = item.TakeManCardId.replace(/(\d{6})\d{4}(\d{4})/, '$1****$2');
          });
          for (var i = 0, lenI = res.data.TakeManList.length; i < lenI; ++i) {
            res.data.TakeManList[i].TakeManPhone = false;
          };
        }

        that.setData({
          contacts: res.data.TakeManList
        });

        app.hideLoadings();
      },
      fail: function () {
        console.log("调用失败");
        app.hideLoadings();
      },
      complete: function (res) {
        that.setData({ isPage: true })
      }
    })
  },
  getActivity: function () {
    var that = this;
    wx.request({
      url: app.serviceurl + '/CustomLine/CustomQueryDiscount',
      header: {
        Authorization: "Bearer " + app.accesstoken
      },
      dataType: 'json',
      success: function (res) {
        that.setData({
          acresult: res.data.Result
        })
      },
      fail: function () {
        console.log("调用失败");
      }
    })
  },
  append: function () {
    wx.navigateTo({
      url: '../../../pages/contact/addContact/addContact',
    })
  },
  returns: function () {
    this.setData({
      mainpage: true,
      contactpage: false,
      appends: false,
      activity: false
    });
  },
  cancel: function () {
    this.setData({
      appends: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getTakeman();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  choosecontact: function (e) {
    this.setData({
      mainpage: false,
      contactpage: true
    });
  },
  chooseactivity: function (e) {
    this.setData({
      mainpage: false,
      activity: true
    });
  },
  confirmactive: function (e) {
    this.setData({
      mainpage: true,
      activity: false
    });
  },
  checkboxChange: function (e) {
    var contacts = this.data.contacts;
    if (contacts == null || typeof (contacts) == undefined) {
      return;
    }
    for (var i = 0, lenI = contacts.length; i < lenI; ++i) {
      if (contacts[i].TakeManId == e.currentTarget.dataset.takeman) {
        contacts[i].TakeManPhone = contacts[i].TakeManPhone == true ? false : true;
      }
      if (contacts[i].TakeManPhone == true) {
        this.setData({
          selected: true
        });
      }
    }
    this.setData({
      contacts: contacts
    });
  },
  chooseconfirm: function (e) {
    var contacts = this.data.contacts;
    var tmp = [];
    var TakeManID = [];
    var count = 0;
    this.setData({
      iscontactschoose: false,
      appends: false
    });
    for (var i = 0, lenI = contacts.length; i < lenI; ++i) {
      if (contacts[i].TakeManPhone == true) {
        count++;
        tmp.push(contacts[i]);
        this.setData({
          iscontactschoose: true
        });
      }
    }
    app.custom = tmp;
    var moeny = count * this.data.price;
    this.setData({
      mainpage: true,
      contactpage: false,
      contactschoose: tmp,
      totalmoeny: moeny
    });
  },
  //活动
  radioChange: function (e) {
    this.setData({
      activiId: e.detail.value,
    });
  },

  eachactive: function (e) {
    var activename = e.currentTarget.dataset.activityname,
      index = e.currentTarget.dataset.index;
    this.data.active.forEach(function (item, i) {
      item.checked = false
    })
    this.data.active[index].checked = true;
    this.setData({
      activename: activename,
      active: this.data.active,
    })
  },


})